package gui.ScheduleTab;
import gui.Interpreter;
import java.util.List;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import common.CommandAnswer;
import common.Tables.Table;

/**
 * this class open in separate thread 
 * 			the auto schedule command to interpreter  
 * @author Orit
 *
 */

public class AutoScheduleTasks implements Runnable{
	Interpreter interpreter;			//interpreter
	public boolean done = false;		//notice if the thread done (to keep drawing the GUI)
	private Display display;			
	private Shell shell;
	CommandAnswer ans;					//the answer for auto schedule
	private String dateTable1;			//the date of first table
	private String dateTable2;			//the date of second table
	private String arriveDate;			//schedule the date of arriving
	
	/**
	 * C'tor  
	 * @param interpreter- to send the command
	 * @param display - of the GUI	
	 * @param shell		- Of the GUI
	 * @param dateTable1 - date of first table
	 * @param dateTable2 - date of second table
	 * @param arriveDate - the date of arriving
	 */
	public AutoScheduleTasks(Interpreter interpreter ,Display display,Shell shell,
			String dateTable1,String dateTable2,String arriveDate) {
		// TODO Auto-generated constructor stub
		this.interpreter = interpreter;
		this.display = display;
		this.shell = shell;
		this.dateTable1 = dateTable1;
		this.dateTable2 = dateTable2;
		this.arriveDate = arriveDate;
	}
	
	/**
	 * run the auto schedule command in separate thread and continue drawing
	 */
	public void run(){
		done = false;
		 Thread thread = new Thread(new Runnable() { //open in separate thread
			@Override
			public void run() {
				autoschedule();
				done = true;
				display.wake();
			}
		 });
		 thread.start();
        while (!done && !shell.isDisposed()) {   //Continue to draw the main GUI. (no black screen)
          if (!display.readAndDispatch())
            display.sleep();
        }
		
	}
	
	/**
	 * the auto command that send to server
	 */
	private void autoschedule() {
		// TODO Auto-generated method stub
		this.ans = interpreter.autoSchedule(dateTable1,dateTable2,arriveDate);
	}
	
	/**
	 * get First Table in Gui (dateTable1)
	 * @return first table of auto Scheduling answer
	 */
	public List<Table> getFirstTable() {
		return ans.getListTableAns();
	}
	
	/**
	 * get Second Table in Gui (the date of dateTable2)
	 * @return second table of auto Scheduling answer
	 */
	public List<Table> getSecTable() {
		return ans.getListTableSecAns();
	}
	
	/**
	 * is thread was done
	 * @return true if the thread finish false otherwise
	 */
	public boolean isDone() {
		return done;
	}
}
